﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Server.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.Coolzon.Server
{
    public class CZEnvironment
    {
        private static Version _version;
        /// <summary>
        /// 获得主程序的版本号
        /// </summary>
        public static Version Version
        {
            get
            {
                if (_version == null)
                {
                    _version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
                }
                return _version;
            }
        }

        /// <summary>
        /// 版本信息-.-
        /// </summary>
        public static string Version2
        {
            get
            {
                return Version.ToString(2);
            }
        }

        /// <summary>
        /// 版本信息-.-.-
        /// </summary>
        public static string Version3
        {
            get
            {
                return Version.ToString(3);
            }
        }

        /// <summary>
        /// 版本信息-.-.-.-
        /// </summary>
        public static string Version4
        {
            get
            {
                return Version.ToString(4);
            }
        }

        /// <summary>
        /// 获取承载服务的宿主ip地址
        /// </summary>
        /// <returns></returns>
        public static string GetHostIP()
        {
            //return "localhost";
            return "127.0.0.1";
        }

        public static int GetHostPort()
        {
            return 7832;
        }

        /// <summary>
        /// 获取承载主服务的端口号
        /// </summary>
        /// <returns></returns>
        public static int GetHostMainServicePort()
        {
            return 8347;
        }

        /// <summary>
        ///  获取承载数据服务的端口号
        /// </summary>
        /// <returns></returns>
        public static int GetHostDataServicePort()
        {
            return 8348;
        }

        /// <summary>
        ///  获取承载同步服务的端口号
        /// </summary>
        /// <returns></returns>
        public static int GetHostSyncServicePort()
        {
            return 8349;
        }

        /// <summary>
        /// 获取host配置信息的文件路径
        /// </summary>
        /// <returns></returns>
        public static string GetHostConfigFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Config\\host.xml");
        }

        /// <summary>
        /// 获取日志配置文件的路径
        /// </summary>
        /// <returns></returns>
        public static string GetLoggerConfigFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Config\\log.xml");
        }

        /// <summary>
        /// 获取数据源的元数据定义文件路径
        /// </summary>
        /// <param name="datasource">数据源名称</param>
        /// <param name="dataset">数据集名称</param>
        /// <returns></returns>
        public static string GetDataSourceMetadataFilePath(string datasource, string dataset)
        {
            return string.Format("{0}Metadata\\DataSource\\{1}.xml", AppDomain.CurrentDomain.BaseDirectory, datasource);
        }

        /// <summary>
        /// 获取方案的配置文件路径,(xml文件地址）
        /// </summary>
        /// <param name="solution"></param>
        /// <returns></returns>
        public static string GetSolutionMetadataFilePath(string solution)
        {
            return string.Format("C:\\Coolzon\\Server\\V{0}\\RuntimeStorage\\Solution\\{1}\\{1}.xml", Version2, solution);
        }

        /// <summary>
        /// 获取方案列表文件的路径
        /// </summary>
        /// <returns></returns>
        public static string GetSolutionListMetadataFilePath()
        {
            return string.Format("C:\\Coolzon\\Server\\V{0}\\{1}", Version2, "RuntimeStorage\\Solution\\Solution.xml");
        }

        /// <summary>
        /// 获取程序的目录地址
        /// </summary>
        /// <returns></returns>
        public static string GetBaseDirectory()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        public static string GetLogDirectory()
        {
            return string.Format("C:\\R2CD\\WEITSM\\Server\\V1.0.0\\Logs\\");
        }

        /// <summary>
        /// 获取Environment配置文件的路径
        /// </summary>
        /// <returns></returns>
        public static string GetEnvironmentFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Config\\Environment.xml");
        }

        #region singleton mode

        private static CZEnvironment s_instance;
        public static CZEnvironment Instance
        {
            get
            {
                if (s_instance == null)
                {
                    s_instance = new CZEnvironment();
                }
                return s_instance;
            }
        }

        #endregion

        /// <summary>
        /// 方案的基本配置信息
        /// 此配置信息来自配置文件的Base节点
        /// </summary>
        public SolutionSetting BaseSolutionSetting { get; private set; }

        /// <summary>
        /// 方案的配置信息
        /// 此配置信息来自各个方案的配置节点
        /// </summary>
        public List<SolutionSetting> SolutionSettings { get; private set; }

        /// <summary>
        /// 初始化,加载配置文件
        /// </summary>
        /// <param name="configFile"></param>
        public void Init(string configFile)
        {
            if (SolutionSettings == null)
            {
                SolutionSettings = new List<SolutionSetting>();
            }
            else
            {
                SolutionSettings.Clear();
            }

            // 从配置文件Environment.xml中加载配置信息

            if (System.IO.File.Exists(configFile))
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(configFile);
                XmlNode baseXmlNode = xmlDocument.SelectSingleNode("/Environment/SolutionSetting/Base");

                XmlNodeList solutionXmlNodeList = xmlDocument.GetElementsByTagName("Solution");

                if (baseXmlNode != null)
                {
                    BaseSolutionSetting = new SolutionSetting();
                    BaseSolutionSetting.DomainInstanceMode = (DomainInstanceMode)Enum.Parse(typeof(DomainInstanceMode), XmlHelper.GetXAttributeValue(baseXmlNode.LastChild, "Value"));
                }

                if (solutionXmlNodeList != null && solutionXmlNodeList.Count > 0)
                {
                    foreach (XmlNode node in solutionXmlNodeList)
                    {
                        SolutionSetting solutionSetting = new SolutionSetting();
                        solutionSetting.SolutionName = XmlHelper.GetXAttributeValue(node, "Name");
                        solutionSetting.DomainInstanceMode = (DomainInstanceMode)Enum.Parse(typeof(DomainInstanceMode), XmlHelper.GetXAttributeValue(node.FirstChild, "Value"));

                        SolutionSettings.Add(solutionSetting);
                    }
                }
            }
        }

        /// <summary>
        /// 获取方案的控制域实例类型，默认返回Multiple
        /// </summary>
        /// <param name="solutionName"></param>
        /// <returns></returns>
        public Settings.DomainInstanceMode GetSolutionDomainInstanceMode(string solutionName)
        {
            // 先从方案自己的配置信息里获取，如果没有再从base节点里获取

            if (SolutionSettings != null && SolutionSettings.Count > 0)
            {
                foreach (SolutionSetting solutionSetting in SolutionSettings)
                {
                    if (solutionSetting.SolutionName.Equals(solutionName))
                    {
                        return solutionSetting.DomainInstanceMode;
                    }
                }
            }
            else
            {
                if (BaseSolutionSetting != null)
                {
                    return BaseSolutionSetting.DomainInstanceMode;
                }
            }

            return DomainInstanceMode.Multiple;
        }


        public string ServerIP { get; set; }
        public int ServerPort { get; set; }
    }
}
